package org.example.fronted.controller;

import org.example.entity.Data;
import org.example.entity.Orders;
import org.example.entity.WebOrders;
import org.example.fronted.service.ShowOrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

@RestController
public class ShowOderController extends HttpServlet {

    ScheduledExecutorService executorService=null;

    private ShowOrderService showOrderService;
    public ShowOderController(ShowOrderService showOrderService){
        this.showOrderService=showOrderService;
    }


    //获取未支付、已支付和已取消订单
    @RequestMapping("/getOrderByStatus")
    public Object getOrderByStatus(HttpServletRequest request,String status) throws ServletException, IOException {

        System.out.println("status"+status);

//        int uid=28;
//        String uId = (String)request.getSession().getAttribute("userId");
        Object userId = request.getSession().getAttribute("userId");
        int uid = (int)userId;


        Map map=new HashMap();
        map.put("o_status",status);
        map.put("o_user_id",uid);

        List<WebOrders> ordersList = showOrderService.getOrdersByStatus(map);
        ordersList.forEach(o->{
            System.out.println(o);
        });
        return ordersList;

    }

    @RequestMapping("/cancelOrder")
    //24小时未支付，订单过期
    public void cancelOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {

    }

    //取消未支付订单
    @RequestMapping("/cancelOrderByoNo")
    public Object cancelOrderByoNo( String oNo) throws IOException {

        System.out.println(oNo);
        boolean flag = showOrderService.cancelOrderByoNo(oNo);
        if (flag){
            Data data=new Data(1,"订单已取消");
            return data;
        }else {
            Data data=new Data(-1,"系统异常，请稍后重试");
            return data;
        }
    }

    @RequestMapping("/payByoNo")
    public Object payByoNo(String oNo) throws IOException {

        List<Orders> ordersList = showOrderService.payByoNo(oNo);
        return ordersList;
    }

}
